﻿using System;
using System.ComponentModel;
using System.Data;
using System.Windows;
using System.Windows.Controls;
using SLS.Controls;
using SLS.ExClassLib.Database;
using SLS.ExClassLib.Events;
using SLS.ExClassLib.MVVM;
namespace SLS.Base
{
    public partial class ExDATA : SLBC
    { 
        FilteringDataGrid MainGridData
        {
            get
            {
                return (FilteringDataGrid)TryFindResource("MainGridData");
            }
        }
        public override void ValidateGDKEY(System.Data.DataRow dwThis)
        {
            base.ValidateGDKEY(dwThis);

        }
        public override void cmbForeignKey_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            base.cmbForeignKey_SelectionChanged(sender, e);
            return;
            try
            {
                ComboBox cb = sender as ComboBox;
                if (cb.SelectedItem == null)
                    return;
                int Selectedindex = cb.SelectedIndex;
                if (Selectedindex < 0)
                    return;
                string FroeignKey = (cb.Tag as string).TrimEnd();
                DataRow dwTemp = ((DataRowView)cb.SelectedItem).Row;
                DataSet setTemp = new DataSet();
                setTemp = (DataSet)DataAccessService.Instance.ImportDataTable(setTemp, dwTemp);
                DataRow dwThis = null;// ActiveDataRow;

                if (dwThis == null)
                    return;
                if (FroeignKey != "")
                {
                    UpdateByForeignKeyWithReason(setTemp, ((DataModel)this.DataContext).SetEnv, dwThis, FroeignKey);
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(this.Name + ".cmbForeignKey_SelectionChanged", ex);
            }
        } 
        public override void SLBC_Loaded(object sender, RoutedEventArgs e)
        {
            #region Not In DesignMode
            if (DesignerProperties.GetIsInDesignMode(this as DependencyObject))
            {
                return;
            }
            #endregion
            
            if(ViewModelBase!=null)
            {
                if(ViewModelBase.ActiveExDataRow!=null)
                {
                    if (ViewModelBase.ActiveExDataRow.TBName == BaseTable)
                        return;
                    ViewModelBase.ActiveExDataRow.BuildSet5Property(BaseTable);
                    foreach (DataTable tbTemp in ViewModelBase.ActiveExDataRow.Set5Property.Tables)
                    {
                        ViewModelBase.ViewDataSet.SetEnv.Tables[tbTemp.TableName].Clear();
                        ViewModelBase.ViewDataSet.SetEnv.Merge(tbTemp);
                    }
                    ViewModelBase.ViewDataSet.SetEnv.AcceptChanges();
                }
            }
            base.SLBC_Loaded(sender, e);
        }
        public virtual void DATAPopUpMenu_Loaded(object sender, RoutedEventArgs e)
        { 
        }
    }
}
